<?php
// +----------------------------------------------------------------------
// | Site:  [ http://www.yzmcms.com]
// +----------------------------------------------------------------------
// | Copyright: 袁志蒙工作室，并保留所有权利
// +----------------------------------------------------------------------
// | Author: YuanZhiMeng <214243830@qq.com>
// +---------------------------------------------------------------------- 
// | Explain: 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用，不允许对程序代码以任何形式任何目的的再发布！
// +----------------------------------------------------------------------

defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_sys_class('page','',0);
yzm_base::load_common('class/xlswriter.class.php');

class hr_diy extends common {
	
	/**
	 * 表格列表
	 */
	public function diy_set(){
		$d = D('hr_diy');
		$action = $_GET['action'] ?? '';
		//添加或者编辑
		if($action=='add'){
			if(is_post()){
				if($_POST['id']){
					$result = $d->update($_POST,['id'=>$_POST['id']]);
				}else{
					$post = $_POST;
					unset($post['yzm_csrf_token']);unset($post['dosubmit']);unset($post['yzm_csrf_token']);unset($post['id']);
					$insert_data['content'] = json_encode($post);
					$insert_data['adminname'] = $_SESSION['adminname'];
					$insert_data['add_time'] = time();
					$insert_data['modified_time'] = time();
					$insert_data['type'] =$type;
					$result = $d->insert($insert_data);
				}
				if($result!==false){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>L('operation_failure')));
				}
			}
			if($_GET['id']){
				$data = $d->where(['id'=>$_GET['id']])->find();
				$data = json_decode($data['field'],true);
			}
			$ids = array_column($data, 'id'); 
			array_multisort($ids, SORT_ASC, $data);
			include $this->admin_tpl('hr/diy_field_list');
			exit;
		}elseif($action=='add_table'){
			$id = $_GET['id']??'';
			$data=['id'=>'','type'=>'','name'=>'','is_download'=>1,'is_upload'=>'','adminids'=>'','adminnames'=>''];
			if(is_post()){
				$post = $_POST;
				$post['add_time'] = time();
				$post['field'] = '{}';
				if($post['id']){
					unset($post['field']);
					$result = $d->update($post,['id'=>$post['id']]);
				}else{
					$result = $d->insert($post);
				}
				if($result!==false){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>L('operation_failure')));
				}
			}
			if($id){
				$data = $d->where(['id'=>$_GET['id']])->find();
				$admin = get_admin_list();
				$adminnames = [];
				foreach(explode(',',$data['adminids']) as $v){
					if($v){
						$adminnames[] = $admin[$v];
					}
				}
				$data['adminnames'] = implode(',',$adminnames);
			}
			return include $this->admin_tpl('hr/diy_field_add_table');
		}
		$data = $d->select();
		$admin = get_admin_list();
		foreach($data as &$v){
			$field = json_decode($v['field'],true);
			$ids = array_column($field, 'id'); 
			array_multisort($ids, SORT_ASC, $field);
			$temp = []; 
			foreach($field as $val){
				$temp[] = $val['title']; 
			}
			$v['field'] = implode(',',$temp);
			$adminnames = [];
			if($v['adminids']){
				foreach(explode(',',$v['adminids']) as $val){
					if($val&&isset($admin[$val])){
						$adminnames[] = $admin[$val];
					}
				}
			}else{
				$adminnames=['全部'];
			}
			$v['adminnames'] = implode(',',$adminnames);
			
		}
		include $this->admin_tpl('hr/diy_set');
	}
	
	//表格字段编辑
	public function diy_field(){
		$action = $_GET['action'];
		$field = $_GET['field']??'';
		$id = $_GET['id'];
		$d = D('hr_diy');
		//echo $id;echo $field;echo $action;exit;
		if((!$action || !$id)&&!is_post()){
			exit('参数错误！');
		}
		$data = $d->where(['id'=>$id])->find();
		$type = $data['type'];
		$data = json_decode($data['field'],true);
		if($action == 'edit'){
			if(is_post()){
				$post = $_POST;
				unset($post['yzm_csrf_token']);unset($post['dosubmit']);unset($post['yzm_csrf_token']);
				$keys = array_keys($data);
				$field = $post['field'];
				unset($post[$field]);
				$data[$field] = $post;
				foreach($data as &$v){
					if(!isset($v['id'])){
						$v['id'] = 0;
					}
				}
				$ids = array_column($data, 'id'); 
				array_multisort($ids, SORT_ASC, $data);
				//print_r($data);exit;
				$insert_data = [
					'field'=>json_encode($data),
				];
				$result = $d->update($insert_data,['id'=>$id]);
				//echo $d->lastsql();exit;
				if($result!==false){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>L('operation_failure')));
				}
			}
			$data = $data[$field];
			include $this->admin_tpl('hr/diy_field_add');
			exit;
		}elseif($action == 'add'){
			include $this->admin_tpl('hr/diy_field_add');
		}elseif($action == 'del'){
			foreach($data as $k=>$v){
				if($v['field']==$field){
					unset($data[$k]);
				}
			}
			$ids = array_column($data, 'id'); 
			array_multisort($ids, SORT_ASC, $data);
			$insert_data = [
				'field'=>json_encode($data),
			];
			//删除表格字段
			$result = $d->update($insert_data,['id'=>$id]);
			//删除表格数据
			$sql = 'UPDATE yzm_hr_diy_content SET content = '."JSON_REMOVE(content, '$.".$field."')".',modified_time='.time().' WHERE type = "'.$type.'"';
			$result2 = $d->query($sql);
			if($result!==false&&$result2!==false){
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json(array('status'=>0,'message'=>L('operation_failure')));
			}
		}
	}

	/**
	 * 表格数据
	 */
	public function diy_list() {
		$d = D('hr_diy_content');
		$type = $_GET['type'] ?? '';
		if(!$type){
			$diy_list = D('hr_diy')->select();
			return include $this->admin_tpl('hr/diy_table_list');
		}
		$action = $_GET['action'] ?? '';
		$diy_list = D('hr_diy')->where(['type'=>$type])->find();
		if($diy_list['adminids']&&$_SESSION['roleid']!=1){
			if(!in_array($_SESSION['adminid'],explode(',',$diy_list['adminids']))){
				return_message(L('no_permission_to_access'), 0);
			}
		}
		
		if(!$diy_list){
			$data = ['code'=>1,'msg'=>'列表配置错误','data'=>[]];
			return_json($data);
		}
		$field = json_decode($diy_list['field'],true);
		$ids = array_column($field, 'id'); 
		array_multisort($ids, SORT_ASC, $field);
		if(!isset($_POST['page'])&&!$action){
			if(!$type){
				$data = ['code'=>1,'msg'=>'type参数错误','data'=>[]];
				return_json($data);
			}
			return include $this->admin_tpl('hr/diy_list');
		}
		//添加或者编辑
		if($action=='add'){
			if(is_post()){
				$post = $_POST;
				unset($post['yzm_csrf_token']);unset($post['dosubmit']);unset($post['yzm_csrf_token']);unset($post['id']);
				$insert_data['content'] = json_encode($post);
				$insert_data['adminname'] = $_SESSION['adminname'];
				$insert_data['modified_time'] = time();
				$insert_data['type'] =$type;
				if(isset($_POST['id'])){
					$result = $d->update($_POST,['id'=>$_POST['id']]);
				}else{
					$insert_data['add_time'] = time();
					$result = $d->insert($insert_data);
				}
				if($result!==false){
					return_json(array('status'=>1,'message'=>L('operation_success')));
				}else{
					return_json(array('status'=>0,'message'=>L('operation_failure')));
				}
			}
			if($_GET['id']){
				$data = $d->where(['id'=>$_GET['id']])->find();
				$content = json_encode($data['content'],true);
			}
			include $this->admin_tpl('hr/diy_add');
			exit;
		}
		
		$page = input('post.page');
		$limit = input('post.limit');
		$of = input('post.field') ? input('post.field') : '';
		$or = input('post.order') ? input('post.order') : '';
		if($of){
			$order = "JSON_EXTRACT(content, '$.".$of."') ".$or;
		}else{
			$order = 'id desc';
		}
		
		$not_field = ['page','limit','field','order','download','yzm_csrf_token','type'];
		
		$where = '1=1 and type ="'.$type.'"';
		foreach($_POST as $k=>$v){
			$v = trim($v);//SELECT * FROM users  WHERE JSON_EXTRACT(info, '$.name') LIKE '%John%'; 
			if(!in_array($k,$not_field)&&$v){
				$where .=' and JSON_EXTRACT(content,"$.'.$k.'") like "%'.$v.'%"';
			}
		}
		if($page){
			$total = $d->where($where)->total();
			$page = new page($total, $limit);
			$data = $d->where($where)->order($order)->limit($page->limit())->select();
			foreach($data as &$v){
				foreach(json_decode($v['content']) as $key=>$val){
					$v[$key]=$val;
				}
				$v['add_time'] = date('y-m-d H:i:s',$v['add_time']);
				$v['modified_time'] = date('y-m-d H:i:s',$v['modified_time']);
			}
			$data = ['code'=>0,'msg'=>'','count'=>$total,'data'=>$data];
			return_json($data);
		}
	}
	
	public function public_edit_field(){
		$d = D('hr_diy_content');
		$post =$_POST;
		$sql = 'UPDATE yzm_hr_diy_content SET content = '."JSON_SET(content, '$.".$post['field']."', '".$post['value']."')".',modified_time='.time().' WHERE id = '.$post['id'];
		$result = $d->query($sql);
		if($result !== false){
			delete_all('JST_CONFIG:*');
			return_json(array('status'=>1,'message'=>L('operation_success')));
		}else{
			return_json(array('status'=>0,'message'=>L('operation_failure')));
		}
	}
	
}